package kr.hkit.androidcomplete02.ch12;

import kr.hyunist.androidcomplete01.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewEx extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_gridview);
		
		GridView grid= (GridView) findViewById(R.id.ch12_grid);
		ImageAdapter adapter = new ImageAdapter(this);
		grid.setAdapter(adapter);
		
		grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(GridViewEx.this, position + "번째 그림 선택", Toast.LENGTH_SHORT).show();
			}
		});
	}
}

class ImageAdapter extends BaseAdapter{
	private Context mContext;
	
	int[] picture = {R.drawable.ccdam, R.drawable.soyang2, R.drawable.ududong, R.drawable.zipdarigol, R.drawable.dongul};
	
	public ImageAdapter(Context mContext) {
		super();
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		return 100;
	}

	@Override
	public Object getItem(int position) {
		return picture[position % 5];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageview;
		if(convertView == null){
			imageview = new ImageView(mContext);
			imageview.setLayoutParams(new GridView.LayoutParams(120, 90));
			imageview.setAdjustViewBounds(false);
			imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
		} else {
			imageview = (ImageView) convertView;
		}
		
		imageview.setImageResource(picture[position%5]);
		return imageview;
	}
	
}